﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using AudioPlayer.GUI.Music;
using AudioPlayer.Plugin.Storage;
namespace AudioPlayer.GUI.List {
	internal class ListDrag : IDisposable {
		#region Variables
		private Form _form;
		private Bitmap _bitmap;
		private IList<ListItem> _list;
		#endregion
		#region Constructor
		public ListDrag(Bitmap bmp, IList<ListItem> list) {
			_list = list;
			_bitmap = bmp;
			this.Init();
		}
		#endregion
		#region Private Voids
		private void Init() {
			this.InitForm();
		}
		private void InitForm() {
			_form = new Form();
			_form.Size = _bitmap.Size;
			_form.BackgroundImage = _bitmap;
			_form.BackgroundImageLayout = ImageLayout.None;
			_form.FormBorderStyle = FormBorderStyle.None;
			_form.AllowTransparency = true;
			_form.Opacity = 0.8;
			_form.ShowIcon = false;
			_form.ShowInTaskbar = false;
			_form.StartPosition = FormStartPosition.Manual;
			_form.WindowState = FormWindowState.Normal;
			_form.TransparencyKey = Color.White;
			_form.MouseUp += new MouseEventHandler(FormMouseUp);
			_form.Deactivate += new EventHandler(FormDeactivate);
			_form.Shown += new EventHandler(FormShown);
			_form.MouseMove += new MouseEventHandler(FormMouseMove);
			_form.FormClosed += new FormClosedEventHandler(FormClosed);

			_form.MinimumSize = _bitmap.Size;
			_form.MaximumSize = _bitmap.Size;
		}

		/// <summary>
		/// Checks, if is over music tree
		/// </summary>
		private bool CheckMusicTree() {
			Point loc = this.Location;

			Form own = this.Form.Owner;
			if (own != null) {
				Control panel = own.GetChildAtPoint(own.PointToClient(loc));
				if (panel != null && panel is AudioPlayer.GUI.Splitter.Split) {
					//Very good
					Control split = panel.GetChildAtPoint(panel.PointToClient(loc));
					if (split != null) {
						Control c = split.GetChildAtPoint(split.PointToClient(loc));
						if (c != null && c is Music.MusicTree) {
							MusicTree tree = (MusicTree)c;
							MusicNode node = tree.HitInfoNode(tree.PointToClient(loc));

							if (node != null) {
								if (node.Tag is IPlaylist) {
									IPlaylist pl = (IPlaylist)node.Tag;
									List<ISong> songList = new List<ISong>();
									for (int i = 0; i < this.List.Count; i++) {
										songList.Add(this.List[i].Song);
									}
									if (Config.Plugins.HasStorage) {
										Config.Plugins.Storage.Put(pl, songList.ToArray());
									}
								}
							}
						}
					}
				}
			}
			return false;
		}
		#endregion
		#region Public Voids
		public void Show() {
			_form.Show();
		}
		public void Show(IWin32Window parent) {
			_form.Show(parent);
		}
		#endregion
		#region Form Events
		private void FormMouseMove(object sender, MouseEventArgs e) {
			_form.Location = _form.PointToScreen(e.Location);
		}
		private void FormShown(object sender, EventArgs e) {
			_form.Capture = true;
		}
		private void FormDeactivate(object sender, EventArgs e) {

		}
		private void FormMouseUp(object sender, MouseEventArgs e) {
			_form.Close();
		}
		private void FormClosed(object sender, FormClosedEventArgs e) {
			CheckMusicTree();
			OnClose();
		}
		#endregion
		#region Custom Events
		public event EventHandler Close;

		protected void OnClose() {
			if (Close != null) {
				Close(this, EventArgs.Empty);
			}
		}
		#endregion
		#region Property
		public Point Location {
			get { return _form.Location; }
			set { _form.Location = value; }
		}
		public IList<ListItem> List {
			get { return _list; }
			set { _list = value; }
		}
		public Form Form {
			get { return _form; }
			set { _form = value; }
		}
		public bool Visible {
			get { return _form.Visible; }
		}
		#endregion

		#region IDisposable Members

		public void Dispose() {
			_form.Dispose();
		}

		#endregion
	}
}
